package com.example.gmailuidemo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class TestView extends Activity {

	ActionBar actionBar;

	Map<String, String> datamap;

	List<Map<String, String>> datalist;
	MyAdapter dataAdapter;
	ListView mListView;
	ModeCallback mCallback;
	ArrayAdapter<String> mainAdapter;
	String[] list = new String[] { "选项1", "选项2", "选项3", "选项4" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.testview);

		mListView = (ListView) findViewById(R.id.mainList);

		mainAdapter = new ArrayAdapter<String>(TestView.this,
				android.R.layout.simple_list_item_1, list);
		mListView.setAdapter(mainAdapter);
		mCallback = new ModeCallback();
		mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
		mListView.setMultiChoiceModeListener(mCallback);

		actionBar = getActionBar();
		actionBar.setDisplayShowTitleEnabled(false);// dont show the title
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

		List<InfoModel> pList = new ArrayList<InfoModel>();

		InfoModel info = new InfoModel("标题1", "111");
		pList.add(info);

		info = new InfoModel("标题2", "222");
		pList.add(info);

		info = new InfoModel("标题3", "333");
		pList.add(info);

		info = new InfoModel("标题4", "444");
		pList.add(info);
		dataAdapter = new MyAdapter(this, pList);

		OnNavigationListener callback = new MyOnNavigationListener();
		actionBar.setListNavigationCallbacks(dataAdapter, callback);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.testview_menu, menu);
		return true;
	}

	//顶部操作栏 导航列表事件
	private class MyOnNavigationListener implements OnNavigationListener {

		@Override
		public boolean onNavigationItemSelected(int itemPosition, long itemId) {

			Toast.makeText(getApplicationContext(), "当前选择了：" + itemPosition,
					Toast.LENGTH_SHORT).show();

			return true;
		}
	}

	//ListView 点击事件
	private class ModeCallback implements ListView.MultiChoiceModeListener {
		@Override
		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

			return false;
		}

		@Override
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			Log.d("andli", "oncreateactionmode");
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.listview_action_mode_menu, menu);

			return true;
		}

		@Override
		public void onDestroyActionMode(ActionMode mode) {

			Log.d("andli", "ondestroyActionMode");

		}

		@Override
		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			Log.d("andli", "onPrepareActionMode");

			return false;
		}

		@Override
		public void onItemCheckedStateChanged(ActionMode mode, int position,
				long id, boolean checked) {

			mListView.setSelection(position);
			Log.d("andli", "position" + position);

		}

	}

}
